<?php

namespace Budget\BudgetBundle\Controller;

use Budget\BudgetBundle\Entity\Income;
use Budget\BudgetBundle\Form\IncomeType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Budget\BudgetBundle\Helper\DateHelper;
use Budget\BudgetBundle\Model\Charts\ChartIncomes;

class IncomeController extends MainController
{
    public function indexAction(Request $request) 
    {
        $user   = $this->getUser();
        $income = new Income();
        $form   = $this->createForm(new IncomeType(), $income);
       
//        if ($request->getMethod() == "POST") {
//             $form->bind($request);
//            
//            if ($form->isValid()) {
//                //var_dump($request); exit;
//                $em = $this->getEntityManager();
//                $income->setUser($user);
//                $income->setCreatedBy($user);
//                $income->setFamily($user->getFamily());
//                $em->persist($income);
//                $em->flush();
//                $this->setPositveMsg('Income added!');
//                return $this->redirect($this->generateUrl('BudgetBudgetBundle_incomes'));
//            } 
//        }    

        return $this->render('BudgetBudgetBundle:Income:index.html.twig', array(
            'form' => $form->createView()
        ));
    }
    
    public function newAction(Request $request) 
    {
        $user     = $this->getUser();
        $income   = new Income();
        $response = new \stdClass;
        $form     = $this->createForm(new IncomeType(), $income);
        $form->bind($request);
        $response->status = true;
        
            
        if (!$form->isValid()) {
            $response->status = false;
            return new JsonResponse($response);
        }
        
        $em = $this->getEntityManager();
        $income->setUser($user);
        $income->setCreatedBy($user);
        $income->setFamily($user->getFamily());
        $em->persist($income);
        $em->flush();

         return new JsonResponse($response);
    }
    
    public function editAction(Request $request) 
    {
        $incomeId    = $request->request->get('id');
        $comment     = $request->request->get('comment');
        $dateCreated = $request->request->get('date_created');
        $amount      = $request->request->get('amount');
        
        $user       = $this->getUser();
        $userFamily = $user->getFamily(); 
        $em         = $this->getEntityManager();
        //$response   = new \stdClass;
        
        $income = $em->getRepository('BudgetBudgetBundle:Income')->findByIdForFamily($userFamily, $incomeId);
        if (!$income) {
            //$response->status = false;
            //return new Response($response);
            return new Response('FALSE');
        }
        
        $income->setAmount($amount);
        $income->setComment($comment);
        $date = \DateTime::createFromFormat(DateHelper::FORMAT_USER, $dateCreated);
        $income->setDateCreated($date);
        $em->persist($income);
        $em->flush();
        //$response->status = true;
        //return new Response($response);
        return new Response('TRUE');
    }
    
    public function deleteAction(Request $request) 
    {
        $id      = $request->request->get('id');
        $user    = $this->getUser();
        $em      = $this->getEntityManager();
        $expense = $em->getRepository('BudgetBudgetBundle:Income')->findByIdForFamily($user->getFamily(), $id);

        if (!$expense) {
            return new Response('FALSE');
        }
        
        $expense->setDateDeleted(new \DateTime());
        $expense->setDeletedBy($user);
        $em->persist($expense);
        $em->flush();
      
        return new Response('TRUE');
    }
    
    public function getIncomesJsonAction(Request $request) 
    {
        $page  = $request->query->get('page');
        $limit = $request->query->get('rows'); 
        $sidx  = $request->query->get('sidx'); 
        $sord  = $request->query->get('sord');
        $dateTo    = $request->query->get('date_to');
        $dateFrom  = $request->query->get('date_from');
        $em    = $this->getEntityManager();
        $user  = $this->getUser();

        $incomes     =  $this->get('budget_income')->getIncomesForFamilyToArray($user->getFamily(), $page, $limit, $sidx, $sord, $dateTo, $dateFrom);
        $incomesJson =  $this->get('budget_income')->getFormatIncomesToJson($incomes);
        $response    = new JsonResponse();
        $response->setData($incomesJson);
        return $response;
    }
    
    public function dataToChartAction(Request $request) 
    {
        $user        = $this->getUser();
        $dateTo      = $request->query->get('date_to');
        $dateFrom    = $request->query->get('date_from');
        //$dateToDB    = DateHelper::convertToYearMonth($dateTo);
        //$dateFromDB  = DateHelper::convertToYearMonth($dateFrom);
        //$incomes     = $this->get('budget_income')->getIncomesForFamilyToArray($user->getFamily(), null, null, 'i.date_created', null, $dateTo, $dateFrom);
        //$incomesJson = $this->get('budget_income')->getFormatIncomesToChartJson($incomes, $dateToDB, $dateFromDB); 
        
        //$chart = new ChartIncomes($this->getEntityManager());
        //$incomes = $chart->getDataForFamilyToArray($user->getFamily(), null, null, 'i.date_created', null, $dateTo, $dateFrom);
        //$incomesJson = $chart->getToDataJson($incomes, $dateToDB, $dateFromDB);
  
        $incomesJson = $this->get('chart')->getIncomesToJson($user, $dateTo, $dateFrom);
       
        $response    = new JsonResponse();
        $response->setData($incomesJson);
        return $response;
    }
    
    public function getTotalSumAction(Request $request) 
    {
        $user         = $this->getUser();
        $em           = $this->getEntityManager();
        $dateTo       = $request->request->get('date_to');
        $dateFrom     = $request->request->get('date_from');
        $response     = new \stdClass;
        
        $total = $this->get('budget_income')->getTotalSumIncomesForFamily($user->getFamily(), $dateTo, $dateFrom);
        if ($total['total']) {
            $response->total = $total['total'];
        } else {
            $response->total = 0.00;
        }
        return new JsonResponse($response);
    }
}